<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Repository\Attraction_info;

use Plugin\Attractions\AttractionManagement\Model\Attraction_info\AttractionInfo;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 信息资讯表 Repository类
 */
class AttractionInfoRepository extends IRepository
{
   public function __construct(
        protected readonly AttractionInfo $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 标题
        if (isset($params['title']) && !empty($params['title'])) {
            $query->where('title', 'like', '%'.$params['title'].'%');
        }

        // 信息内容
        if (isset($params['intro']) && !empty($params['intro'])) {
            $query->where('intro', '=', $params['intro']);
        }

        // 信息分组1活动2优惠3交通
        if (isset($params['group']) && !empty($params['group'])) {
            $query->where('group', '=', $params['group']);
        }

        // 类型id
        if (isset($params['type_id']) && !empty($params['type_id'])) {
            $query->where('type_id', '=', $params['type_id']);
        }

        // 点位id
        if (isset($params['spot_id']) && !empty($params['spot_id'])) {
            $query->where('spot_id', '=', $params['spot_id']);
        }

        // 封面id
        if (isset($params['cover_id']) && !empty($params['cover_id'])) {
            $query->where('cover_id', '=', $params['cover_id']);
        }

        // 是否显示状态1正常2停止3时间字段决定,优先级高于时间控制
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 排序
        if (isset($params['sort']) && !empty($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 开始时间
        if (isset($params['start_time']) && !empty($params['start_time']) && is_array($params['start_time']) && count($params['start_time']) == 2) {
            $query->whereBetween(
                'start_time',
                [ $params['start_time'][0], $params['start_time'][1] ]
            );
        }

        // 结束时间
        if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time']) && count($params['end_time']) == 2) {
            $query->whereBetween(
                'end_time',
                [ $params['end_time'][0], $params['end_time'][1] ]
            );
        }

        // 扩展信息
        if (isset($params['extension']) && !empty($params['extension'])) {
            $query->where('extension', '=', $params['extension']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}